﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Datalayer;
using Common;
using System.IO;
using System.Drawing;

namespace Portal_KonTum.Admin
{
    public partial class NewsNew : System.Web.UI.Page
    {
        IQueryMgr iq;
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["NewsID"] = null;
            Session["NewsID"] = Request.QueryString["NewsID"];

            if (!IsPostBack)
            {
                iq = QueryMgr.GetDbInstance();
                List<Category> cate = iq.Ca_GetAllCate();
                DL_cate.DataSource = cate;
                DL_cate.DataValueField = "CateID";
                DL_cate.DataTextField = "CategoryName";
                DL_cate.DataBind();

                if (Session["NewsID"] == null)
                {

                }
                else
                {
                    bin_edit();
                    bt_save.Text = "Save";
                }
            }
        }

        protected void bt_save_Click(object sender, EventArgs e)
        {
            iq = QueryMgr.GetDbInstance();
            if (Session["NewsID"] == null)
            {
                // thêm mới
                iq.News_NewNew(set_New());
                Response.Redirect("NewsMn.aspx?Notes=StrNew&Active=true");
            }
            else
            {
                // sua doi
                Guid gd = MethodCommon.Get_Guid(Session["NewsID"].ToString());
                iq.News_EditNew(set_New(gd));
                Response.Redirect("NewsMn.aspx?Notes=StrEdit&Active=true");
            }
        }

        protected void bt_cancel_Click(object sender, EventArgs e)
        {

        }

        void Fill_DataEdit(New bg)
        {
            txt_title_en.Text = bg.NewTitle_en;
            CKEditor2.Text = bg.NewContent_en;
            txt_introl_en.Text = bg.NewIntro_en;
            txt_title.Text = bg.NewTitle;
            txt_source.Text = bg.Source;
            CKEditor1.Text = bg.NewContent;
            
            DL_cate.SelectedValue = bg.CateID.ToString();
            chk_hienthi.Checked = bg.IsShow;
            if(bg.IsHot==true)
                chk_hot.Checked = (bool)bg.IsHot;
            txt_intro.Text = bg.NewIntro;
            
            if (bg.Image != "")
            {
                showPhoto(bg.Image);
            }
        }

        void bin_edit()
        {
            New bg = new New();
            iq = QueryMgr.GetDbInstance();
            bg = iq.News_GetNewByID(new Guid(Session["NewsID"].ToString()));
            Fill_DataEdit(bg);
        }

        /// <summary>
        /// Sửa chữa bug
        /// </summary>
        /// <param name="bg"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        New set_New(Guid id)
        {
            string iduser = Session["IDName"].ToString();

            New bg = new New();
            bg.CateID = MethodCommon.Get_Guid(DL_cate.SelectedValue.ToString());
            bg.CreateBy = MethodCommon.Get_Guid(iduser);//người dùng tạo
            bg.ModifyBy = MethodCommon.Get_Guid(iduser);//người dùng modify
            bg.CreateDate = DateTime.Now;
            bg.Image = Image_hdd.AlternateText;
            bg.IsDeleted = false;
            bg.IsShow = chk_hienthi.Checked;
            bg.ModifyDate = DateTime.Now;
            bg.NewContent = CKEditor1.Text;
            bg.NewIntro = txt_intro.Text;
            bg.NewTitle = txt_title.Text;
            bg.Source = txt_source.Text;

            bg.NewTitle_en = txt_title_en.Text;
            bg.NewContent_en = CKEditor2.Text;
            bg.NewIntro_en =txt_introl_en.Text ;
            
            bg.IsHot = chk_hot.Checked ;

            if (id != null) //sửa chữa
                bg.NewID = id;
            return bg;
        }

        /// <summary>
        /// thêm mới
        /// </summary>
        /// <param name="bg"></param>
        /// <returns></returns>
        New set_New()
        {
            New bg = new New();
            string iduser = Session["IDName"].ToString();

            bg.CateID = MethodCommon.Get_Guid(DL_cate.SelectedValue.ToString());
            bg.CreateBy = MethodCommon.Get_Guid(iduser);//người dùng tạo
            bg.ModifyBy = MethodCommon.Get_Guid(iduser);//người dùng modify
            bg.CreateDate = DateTime.Now;
            bg.Image = Image_hdd.AlternateText;
            bg.IsDeleted = false;
            bg.IsShow = chk_hienthi.Checked;
            bg.ModifyDate = DateTime.Now;
            bg.NewContent = CKEditor1.Text;
            bg.NewIntro = txt_intro.Text;
            bg.NewTitle = txt_title.Text;
            bg.Source = txt_source.Text;
            bg.IsHot = chk_hot.Checked;

            bg.NewTitle_en = txt_title_en.Text;
            bg.NewContent_en = CKEditor2.Text;
            bg.NewIntro_en = txt_introl_en.Text;
            bg.NewID = Guid.NewGuid();
            return bg;
        }

        protected void bt_upload_Click(object sender, EventArgs e)
        {
            string ANH = Path.GetFileName(FileUpload1.PostedFile.FileName);
            General gen = new General();
            if (ANH != "")
            {
                HttpPostedFile img = FileUpload1.PostedFile;
                if (!gen.IsValidImagesFile(img))
                {
                    lb_note.Visible = true;
                    lb_note.Text = ("File đại diện không phải file ảnh cho phép.");
                    return;
                }
                if (!gen.checkFilesize(img))
                {
                    lb_note.Visible = true;
                    lb_note.Text = ("File ảnh đại diện không lớn hơn 512 KB.");
                    return;
                }

                ANH = gen.Upload(img, "", "Admin\\Uploads\\Images\\");
                if (ANH == "")
                {
                    lb_note.Visible = true;
                    lb_note.Text = ("Không thể upload file ảnh đại diện.");
                    return;
                }
                else
                {
                    lb_note.Visible = false;
                    gen.ResizeImage(Server.MapPath("~\\Admin\\Uploads\\Images\\" + ANH), null, 200, 150, true);
                    showPhoto(ANH);
                }
            }
        }

        void showPhoto(string namephoto)
        {
            Image_hdd.Visible = true;
            Image_hdd.AlternateText = namephoto;
            Image_hdd.ImageUrl = "~/Admin/Uploads/Images/" + namephoto;
        }
    }
}